package {
	import flash.display.*;
	import flash.events.*;
	import flash.ui.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.text.StaticText;
	import flash.utils.Timer;
	import com.combiform.core.*;
	import com.combiform.harness.Combi_Harness;
	
	
	public class Main extends MovieClip {
		//globals
		const WIDTH:int=600, HEIGHT:int=300;
		const GS_PLAYING:int=1
		const GS_END:int=2

		var gstate=GS_PLAYING;
		
		//api
		var ioHandler:myIO_Handler;
		var cHarness:Combi_Harness;
		var co:Combine_Obj;

		var square1val:int=0;
		var square2val:int=0;
		var square3val:int=0;
		var square4val:int=0;

		//gui
		var p1score:MovieClip;
		var p2score:MovieClip;
		var p3score:MovieClip;
		var p4score:MovieClip;
		
		var p1text:String="P1="
		var p2text:String="P2="
		var p3text:String="P3="
		var p4text:String="P4="
		
		var debug:Boolean=false;
		
		public function Main(){
			this.addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		function init(e:Event){
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			
			
			//Combiform api ** note that in the released version you probably want to take out the combiharness
			ioHandler = new myIO_Handler(this.stage,true,false);
//			cHarness= new Combi_Harness(this.stage);
//			addChild(cHarness);
			co=new Combine_Obj();

			//listeners
//			cHarness.addEventListener(IR_Event.INPUT, irHandler);
			ioHandler.addEventListener(IR_Event.INPUT, irHandler);
			addEventListener(Event.ENTER_FRAME, onFrame);
			
			//gui
			p1score=new p1_mc();
			p1score.x=50;
			p1score.y=50;
			p1score.dtext.text=p1text
			addChild(p1score);
			p2score=new p1_mc();
			p2score.x=350;
			p2score.y=50;
			p2score.dtext.text=p2text
			addChild(p2score);
			p3score=new p1_mc();
			p3score.x=50;
			p3score.y=300;
			p3score.dtext.text=p3text
			addChild(p3score);
			p4score=new p1_mc();
			p4score.x=350;
			p4score.y=300;
			p4score.dtext.text=p4text;
			addChild(p4score);
		
		}

		//main loop: ioHandler needs to call frameH so make sure this is in your main loop
		function onFrame(e:Event) : void{
			ioHandler.frameH();
			randomlyAdjSquares();
		}
		
		function startGame():void{
         ioHandler.sendEvent(new IR_Event(IR_Event.REQUEST,IR_Event.IR_LED,IR_Event.IR_LED_ON,0 ));
         ioHandler.sendEvent(new IR_Event(IR_Event.REQUEST,IR_Event.IR_LED1,IR_Event.IR_LED_ON,1 ));
         ioHandler.sendEvent(new IR_Event(IR_Event.REQUEST,IR_Event.IR_LED2,IR_Event.IR_LED_ON,2 ));
         ioHandler.sendEvent(new IR_Event(IR_Event.REQUEST,IR_Event.IR_LED3,IR_Event.IR_LED_ON,3 ));
		}

		function randomlyAdjSquares():void{
			if (gstate==GS_PLAYING){
				square1val=(square1val+1)%100;
				square2val=(square2val+5)%100
				square3val=(square3val+7)%100;
				square4val=(square4val+24)%100;
				p1score.dtext.text="P1="+square1val;
				p2score.dtext.text="P2="+square2val;
				p3score.dtext.text="P3="+square3val;
				p4score.dtext.text="P4="+square4val;
			}
		}

      //irHandler is how your game will process IR_Events
		function irHandler(e:IR_Event):void {
			
			
			switch(e.key){
				case IR_Event.IR_OTHER:
//					if (e.os=="p"){
						printSquares();
//					}
				case IR_Event.IR_ACTIONBUTTON:
					trace ("Main:"+e.controllerNum+":KNOB e.IR_ACTIONBUTTON val="+e.val)
					break;
				case IR_Event.IR_LEFTBUTTON:
//					trace ("Main:e.IR_LEFTBUTTON val="+e.val)
					break;
				case IR_Event.IR_RIGHTBUTTON:
//					trace ("Main:e.IR_RIGHTBUTTON val="+e.val)
					break;
				case IR_Event.IR_DOWNBUTTON:
				
//					trace ("Main:e.IR_DOWNBUTTON val="+e.val)
					break;
				case IR_Event.IR_UPBUTTON:
//					trace ("Main:e.IR_UPBUTTON val="+e.val)
					break;
				case IR_Event.IR_PRECOMBINEBUTTON:
//					trace ("Main:e.IR_PRECOMBINEBUTTON val="+e.val)
					break
				case IR_Event.IR_LED:
				case IR_Event.IR_LED1:
				case IR_Event.IR_LED2:
				case IR_Event.IR_LED3:
					ioHandler.sendEvent(e);
					trace("Main sent LED event");
					break;
				case IR_Event.IR_COMBINEBUTTON:
					trace ("Main:e.IR_COMBINEBUTTON e.tostring="+e.toString())
//					e.combine_obj;
					if (e.combine_obj.numberOfConnectedPairs()>3){					
						gstate=GS_END;
					}
					break;
				default: break;
			}
		}
		
		function printSquares(){
			trace ("sq1="+square1val+" sq2="+square2val+" sq3="+square3val+" sq4="+square4val);
		}

	}
}